package com.ruoyi.mall.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.domain.bo.AddressBo;
import com.ruoyi.mall.domain.vo.AddressVo;
import com.ruoyi.mall.domain.Address;
import com.ruoyi.mall.mapper.AddressMapper;
import com.ruoyi.mall.service.IAddressService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 地址Service业务层处理
 *
 * @author ztx
 * @date 2022-12-04
 */
@RequiredArgsConstructor
@Service
public class AddressServiceImpl implements IAddressService {

    private final AddressMapper baseMapper;

    /**
     * 查询地址
     */
    @Override
    public AddressVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询地址列表
     */
    @Override
    public TableDataInfo<AddressVo> queryPageList(AddressBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Address> lqw = buildQueryWrapper(bo);
        Page<AddressVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询地址列表
     */
    @Override
    public List<AddressVo> queryList(AddressBo bo) {
        LambdaQueryWrapper<Address> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Address> buildQueryWrapper(AddressBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Address> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, Address::getUserId, bo.getUserId());
        lqw.eq(bo.getDeptId() != null, Address::getDeptId, bo.getDeptId());
        lqw.eq(StringUtils.isNotBlank(bo.getAppid()), Address::getAppid, bo.getAppid());
        lqw.eq(bo.getUid() != null, Address::getUid, bo.getUid());
        lqw.like(StringUtils.isNotBlank(bo.getName()), Address::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getTel()), Address::getTel, bo.getTel());
        lqw.eq(StringUtils.isNotBlank(bo.getArea()), Address::getArea, bo.getArea());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), Address::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getLabel()), Address::getLabel, bo.getLabel());
        lqw.eq(bo.getLongitude() != null, Address::getLongitude, bo.getLongitude());
        lqw.eq(bo.getLatiude() != null, Address::getLatiude, bo.getLatiude());
        lqw.eq(StringUtils.isNotBlank(bo.getIsDefault()), Address::getIsDefault, bo.getIsDefault());
        lqw.eq(StringUtils.isNotBlank(bo.getPcode()), Address::getPcode, bo.getPcode());
        return lqw;
    }

    /**
     * 新增地址
     */
    @Override
    public Boolean insertByBo(AddressBo bo) {
        Address add = BeanUtil.toBean(bo, Address.class);
        validEntityBeforeSave(add);
        handleBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 保存前的处理
     * @param entity
     */
    private void handleBeforeSave(Address entity) {
        //如果有数据权限 将部门id和用户id录入
        LoginUser loginUser = LoginHelper.getLoginUser();
        entity.setDeptId(loginUser.getDeptId());
        entity.setUserId(loginUser.getUserId());
    }

    /**
     * 修改地址
     */
    @Override
    public Boolean updateByBo(AddressBo bo) {
        Address update = BeanUtil.toBean(bo, Address.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Address entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除地址
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
